package com.example.nbshangpping.buy;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nbshangpping.R;
import com.example.nbshangpping.goods.GoodsBean;
import com.example.nbshangpping.login.UserBean;
import com.example.nbshangpping.utils.BaseActivity;
import com.example.nbshangpping.utils.EditUserDialog;
import com.example.nbshangpping.utils.SaveUserUtils;
import com.example.nbshangpping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuyActivity extends BaseActivity {
    //    定义按钮对象
    private UserBean.DataBean userInfo;  //之前登录时保存的用户信息
    GoodsBean.DataBean dataBean;

    private RadioButton zfbRb,wxRb;
    private ImageView nameIv,telIv,addressIv,backIv;
    private TextView nameTv,telTv,addressTv;
    private Button cancelBtn,payBtn;

    double price;
    int num;
    int commodityid;
    TextView moneyTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy);
        userInfo = SaveUserUtils.getUserInfo(this); //获取到保存的登录信息
        initView();
        setListener();
        setView();
    }

    private void setListener() {
        nameIv.setOnClickListener(listener);
        telIv.setOnClickListener(listener);
        addressIv.setOnClickListener(listener);
        backIv.setOnClickListener(listener);
        cancelBtn.setOnClickListener(listener);
        payBtn.setOnClickListener(listener);
    }

    private void setView() {
        nameTv.setText(userInfo.getName());
        telTv.setText(userInfo.getPhone());
        addressTv.setText(userInfo.getAddress());
    }

    private void initView() {
        //        初始化按钮对象
        zfbRb = (RadioButton)findViewById(R.id.buy_zfb);
        wxRb = (RadioButton)findViewById(R.id.buy_wx);
        nameIv=findViewById(R.id.buy_iv_name);
        telIv=findViewById(R.id.buy_iv_tel);
        backIv=findViewById(R.id.buy_back);
        addressIv=findViewById(R.id.buy_iv_address);
        nameTv=findViewById(R.id.buy_tv_name);
        telTv=findViewById(R.id.buy_tv_tel);
        addressTv=findViewById(R.id.buy_tv_address);
        cancelBtn=findViewById(R.id.buy_btn_cancel);
        payBtn=findViewById(R.id.buy_btn_pay);
        //        获取数量与价格并输出
//        String s=String.valueOf(getIntent().getCharSequenceExtra("price"));
//        price=Double.parseDouble(s);
//        s=String.valueOf(getIntent().getCharSequenceExtra("num"));
//        num=Integer.parseInt(s);
//        s=String.valueOf(getIntent().getCharSequenceExtra("id"));
//        commodityid=Integer .parseInt(s);
        moneyTv=findViewById(R.id.buy_tv_money);
        moneyTv.setText( price*num+"");
        //        给按钮对象设置监听器
        zfbRb.setOnClickListener(Listener1);
        wxRb.setOnClickListener(Listener1);
        settotalprice();
    }

    private void settotalprice() {
        int flag=ispaytype();

        if (flag==0) {
            double price= dataBean.getPrice();
            String s=String.valueOf(getIntent().getCharSequenceExtra("num"));
            int count=Integer.parseInt(s);
            moneyTv.setText(price*count+"");
        }
        if (flag==1) {
            Double totalprice=0.0;
            List<CartGoodsBean.DataBean> selectdatas;
            selectdatas= (List<CartGoodsBean.DataBean>) getIntent().getSerializableExtra("selectdatas");
            for(int i=0;i<selectdatas.size();i++){
                CartGoodsBean.DataBean cartdataBean=selectdatas.get(i);
                double oneprice=cartdataBean.getTotalprices();
                totalprice=totalprice+oneprice;
            }
            moneyTv.setText( totalprice+"");
        }
    }
    private int ispaytype() {
        dataBean= (GoodsBean.DataBean) getIntent().getSerializableExtra("selectdata");
        if(dataBean!=null) {
            return 0;
        }
        return 1;
    }


    View.OnClickListener Listener1=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id=v.getId();
            switch (id){
                case R.id.buy_zfb:
                    Toast.makeText(BuyActivity.this,"支付宝支付",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.buy_wx:
                    Toast.makeText(BuyActivity.this,"微信支付",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.buy_iv_name:
                    String hint ="请输入:";
                    showEditUserDialog(hint,nameTv);
                    break;
                case R.id.buy_iv_tel:
                    hint ="请输入:";
                    showEditUserDialog(hint,telTv);
                    break;
                case R.id.buy_iv_address:
                    hint ="请输入:";
                    showEditUserDialog(hint,addressTv);
                    break;
                case R.id.buy_back:
                    finish();
                    break;
                case R.id.buy_btn_cancel:
                    finish();
                    break;
                case R.id.buy_btn_pay:
                    int flag=2;
                    flag=ispaytype();
                    if(flag==0){
                        buy1();//直接购买
                    }
                    else if(flag==1){
                        buy2();//购物车购买
                    }
                    break;
            }
        }
    };
    private void showEditUserDialog(String hint,TextView tv) {
//        1.创建对话框对象
        EditUserDialog dialog = new EditUserDialog(this);
//        2.显示对话框
        dialog.show();
//        3.设置参数
        dialog.setDialogWidth();
        dialog.setTipHint(hint);
        dialog.setOnEnsureListener(new EditUserDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String msg) {
                tv.setText(msg);
                dialog.cancel();
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:   //登录成功
                Toast.makeText(this,"购买成功！",Toast.LENGTH_SHORT).show();
                //跳转
                finish();
                break;
            default:   //其他，登录失败
                Toast.makeText(this,"购买失败！",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void buy1() {
        double price=dataBean.getPrice();
        int count = Integer.parseInt(String.valueOf(getIntent().getCharSequenceExtra("num")));
        int commodityid=dataBean.getId();
        moneyTv.setText( price*count+"");

        Map<String,String>map=new HashMap<>();
        map.put("count",count+"");
        map.put("userId",userInfo.getId()+"");
        map.put("commodityId",commodityid+"");
        postNetwork(URLUtils.insertBought_url,map);
    }

    class Good{
        int id;
        int userId;
        int commodityId;
        int count;

        public Good(int id,int userId,int commodityId,int count){
            this.id=id;
            this.userId=userId;
            this.commodityId=commodityId;
            this.count=count;
        }
    }
    private void buy2() {
        List<CartGoodsBean.DataBean> selectdatas;
        selectdatas= (List<CartGoodsBean.DataBean>) getIntent().getSerializableExtra("selectdatas");
        List<Good>list=new ArrayList<>();
        for(int i=0;i<selectdatas.size();i++){
            CartGoodsBean.DataBean dataBean=selectdatas.get(i);
            Good good=new Good(dataBean.getId(),userInfo.getId(),dataBean.getCommodityId(),dataBean.getCount());
            list.add(good);
            //            Map<String,String>map=new HashMap<>();
//            map.put("id",dataBean.getId()+"");
//            map.put("userId",userInfo.getId()+"");
//            map.put("commodityId",dataBean.getCommodityId()+"");
//            map.put("count",dataBean.getCount()+"");
        }
        Gson gson=new Gson();
        String s=gson.toJson(list);
        postNetwork_cart(URLUtils.orderShopping_url,s);
    }
}